<template>
  <div class="app-container">
    <dk-query
      ref="nxIB"
      :searchForm="queryParams"
      :inputList="inputList"
      :queryTable="click().handleQuery"
      labelWidth="110"
    >
    </dk-query>
    <dk-table
      ref="table"
      :column="column"
      :pagination="paginationObjs"
      :tableData="tableData"
      :height="tableHeight"
    >
    <template #imgUrl="{ scope }">
        <video
          v-if="['mp4'].includes(scope.imgUrl.split('.')[1])"
          :src="domain + scope.imgUrl"
          controls="controls"
          style="width: 50px; height: 50px"
        />
        <el-image
          v-else
          style="width: 50px; height: 50px"
          :src="domain + scope.imgUrl"
          :preview-src-list="[domain + scope.imgUrl]"
          :fit="'cover'"
        >
        </el-image>
      </template>
    </dk-table>
  </div>
</template>

<script>
// 接口api：first step
import {
  getBannerList,
  editBannerStatus,
  delBanner,
} from '@/api/merchant-store-manage/rotation-manage.js';
import { imgDomain } from '@/configs/env';

export default {
  data() {
    return {
      loading: true,
      domain: imgDomain,
      // 提示弹框
      isDisabledVisiable: false,
      // 规程toolbar：two step
      inputList: [
        {
          type: 'input',
          placeholder: '请输入',
          label: '轮播图名称',
          name: 'bannerName',
        },
        {
          type: 'select',
          placeholder: '请选择',
          label: '状态',
          name: 'status',
          optionList: [
            {
              name: '正常',
              value: 0,
            },
            {
              name: '禁用',
              value: 1,
            },
          ],
        },
        {
          type: 'daterange',
          placeholder: '选择创建时间',
          label: '创建时间',
          name: 'createTime',
          fun: (row) => {
            this.queryParams.createTime[0] = `${row[0]} 00:00:00`;
            this.queryParams.createTime[1] = `${row[1]} 23:59:59`;
          },
        },
        {
          type: 'common-button',
          isShowAdd: true,
          hasPermissObj: {
            add: () => `${this.urlObject.permissPrefix}add`,
          },
          searchFun: (searchForm) => {
            this.queryParams = searchForm;
            this.click().handleQuery();
          },
          resetFun: () => {
            this.click().resetQuery();
          },
          addFun: () => {
            this.click().handleAdd();
          },
        },
      ],
      // 规程表头：shree step
      column: [
        {
          value: 'sort',
          name: '排序',
        },
        {
          value: 'bannerName',
          name: '轮播图名称',
        },
        {
          value: 'imgUrl',
          name: '轮播图',
          type: 'image',
          slot: true,
        },
        {
          value: 'bannerType',
          name: '类型',
          stateList: [
            {
              name: '图片',
              value: 0,
            },
            {
              name: '视频',
              value: 1,
            },
          ],
        },
        {
          value: 'status',
          name: '状态',
          stateList: [
            {
              name: '正常',
              value: 0,
            },
            {
              name: '禁用',
              value: 1,
            },
          ],
        },
        {
          value: 'createTime',
          name: '创建时间',
          fun: (row) => {
            this.queryParams.create[0] = `${row[0]} 00:00:00`;
            this.queryParams.submitTime[1] = `${row[1]} 23:59:59`;
          },
        },
        {
          width: 320,
          value: 'id',
          name: '管理',
          fixed: 'right',
          type: 'button',
          buttonList: [
            {
              type: 'no-tooltip',
              name: '禁用',
              fun: (row) => {
                this.click().handleStatus(row);
              },
              showFun: (row) => {
                return row.status === 0;
              },
              color: 'text',
              hasPermiss: () => `${this.urlObject.permissPrefix}handleStatus`,
            },
            {
              type: 'no-tooltip',
              name: '解禁',
              fun: (row) => {
                this.click().handleStatus(row);
              },
              showFun: (row) => {
                return row.status !== 0;
              },
              color: 'text',
              hasPermiss: () => `${this.urlObject.permissPrefix}handleStatus`,
            },
            {
              type: 'no-tooltip',
              name: '编辑',
              fun: (row) => {
                this.click().handleUpdate(row);
              },
              color: 'text',
              hasPermiss: () => `${this.urlObject.permissPrefix}edit`,
            },
            {
              type: 'no-tooltip',
              name: '删除',
              fun: (row) => {
                this.click().handleDelete(row);
              },
              color: 'text',
              hasPermiss: () => `${this.urlObject.permissPrefix}del`,
            },
          ],
        },
      ],
      // 分页器
      paginationObjs: {
        paginationChange: function () {
          this.network().getList(false);
        }.bind(this),
        page: 1,
        limit: 10,
        total: 0,
        loading: false,
      },
      // 查询参数
      queryParams: {},
      // 表格数据
      tableData: [],
      // url对象：four step
      urlObject: {
        permissPrefix: 'merchant-store-manage:rotation-manage:',
        routePathPrefix: '/merchant-store-manage/rotationManage/',
      },
      // 接口存储对象：five step
      interfaceObj: {
        getBanListApiName: getBannerList,
        delApiName: delBanner,
      },
    };
  },
  created() {
    this.network().getList();
    this.getTableHeight.call(this);
  },
  methods: {
    click() {
      return {
        // 搜索
        handleQuery: async () => {
          this.paginationObjs.page = 1;
          this.network().getList();
        },
        // 重置
        resetQuery: async () => {
          this.queryParams = this.$options.data().queryParams;
          this.paginationObjs = this.$options.data().paginationObjs;
          this.network().getList();
        },
        // 禁用/解禁
        handleStatus: async (row) => {
          const text = row.status == 0 ? '禁用' : '恢复';
          this.deleteMessageBox({
            name: `轮播图名称：${row.bannerName || ''}`,
            message: `是否确认${text}？`,
            prompt: `${text}提示`,
          }).then(() => {
            this.network().handleStatus(row);
          });
          // this.$router.push({
          //   path: `${this.urlObject.routePathPrefix}detail/${row.id}`,
          // });
        },
        // 新增
        handleAdd: async () => {
          console.log(`${this.urlObject.routePathPrefix}edit`);

          this.$router.push({
            path: `${this.urlObject.routePathPrefix}edit`,
          });
        },
        // 编辑
        handleUpdate: async (row) => {
          console.log(row.id)
          this.$router.push({
            path: `${this.urlObject.routePathPrefix}edit/${row.id}`,
          });
        },
        // 删除
        handleDelete: async (row) => {
          this.deleteMessageBox({
            name: `轮播图名称：${row.bannerName || ''}`,
            message: '是否确认删除该轮播图？',
          }).then(() => {
            this.network().handleDelete(row.id);
          });
        },
      };
    },
    network() {
      return {
        getList: async () => {
          const params = this.deepCopy(this.queryParams);
          const obj = {
            ...params,
            pageNum: this.paginationObjs.page,
            pageSize: this.paginationObjs.limit,
            beginTime: this.queryParams.createTime?.[0],
            afterTime: this.queryParams.createTime?.[1],
            createTime: undefined,
          };

          this.loading = true;
          try {
            const { getBanListApiName } = this.interfaceObj;
            const data = await getBanListApiName(obj);
            this.loading = false;
            this.tableData = data.rows;
            this.paginationObjs.total = data.total;
          } catch (error) {
            this.loading = false;
          }
        },
        handleDelete: async (id) => {
          console.log(id);
          const { delApiName } = this.interfaceObj;
          const { code } = await delApiName(id);
          if (code === 0) {
            this.$modal.msgSuccess('删除成功');
          }
          this.network().getList();
        },
        handleStatus: async (row) => {
          const text = row.status == 0 ? '禁用' : '恢复';
          const { code } = await editBannerStatus({
            id: row.id,
            status: row.status ^ 1,
          });

          if (code === 0) {
            this.$modal.msgSuccess(`${text}成功`);
          }
          this.network().getList();
        },
      };
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-message-box__title span {
  position: relative;
  left: 37px !important;
}

::v-deep .cell {
  .el-button {
    padding-right: 10px;
  }
}
</style>
